home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4605 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: newspost1.alt.net!usenet
  2. From: walth@netcom.com (Walt Howard)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Copy constructing an already default constructed object
  5. Date: Wed, 31 Jan 1996 08:18:35 GMT
  6. Organization: AltNet - Affordable Usenet Access - http://www.alt.net
  7. Message-ID: <4en8l1$35e@tofu.alt.net>
  8. References: <4e906b$stk@elaine32.Stanford.EDU> <4eal0n$hgq@dawn.mmm.com> <3108ef14.340699@nntp> <4ebehj$5i7@news.bridge.net> <4edr59$87h@elaine11.Stanford.EDU>
  9. Reply-To: walth@netcom.com
  10. X-Newsreader: Forte Agent .99c/32.126
  11.  
  12. On 27 Jan 1996 10:33:13 -0800, brien@leland.Stanford.EDU (brien
  13. oberstein) wrote:
  14.  
  15. >David Byrden <100101.2547@compuserve.com> writes:
  16. >
  17. >
  18. >>>>  A::operator =(const A& other)
  19. >>>>  {
  20. >>>>    b0 = other.b0 ;
  21. >>>>    b1 = other.b1;
  22. >>>>  }
  23. >
  24. >>>> To me this seems like a major pain in the butt.
  25. >
  26. >>That's precisely why the compiler will automatically generate a public 
  27. >>assignment operator with this behaviour, if you don't bother to write 
  28. >>one.
  29. >
  30. >Bzz.  Try again king shit.  The compiler generates memberwise assignment.
  31. >
  32. >
  33.  
  34. Huh? That's what he is saying. b0 and b1 are MEMBERS of A and
  35. the compiler will automatically generate:
  36.  
  37.      b0 = other.b0 ;
  38.      b1 = other.b1;
  39.  
  40. i.e., memberwise assignment.
  41.  
  42. >>>>  Do you have to overload = for all the contained operators too?
  43. >
  44.  
  45. No, because there is always an = operator for a class, be it compiler
  46. generated, explicitly written or hidden as a private member.
  47.  
  48. Pointers are the buggaboo here. Usually you don't want the POINTERS
  49. copied. You want what they REFER TO copied, i.e., the REFERANT. In
  50. that case you have to explicitly code to make that happen.
  51.  
  52. It's the same in C. You can't copy strings by = ing their pointers right? 
  53. You have to use strcpy(destination, source )
  54.  
  55. >>>>  All I really want to do is have the copy constructor invoked on the 
  56. >>>> piece that piece of memory.  Why can't this be done?
  57.  
  58. Because = an object, means = all it's components, not, = it's memory.
  59. In C++ an OBJECT is not necessarily just memory. I can be a "connection" 
  60. to another machine. It can be an open file. It can be a screen
  61. capture. It could be a TCP/IP port. Whatever. The only thing the
  62. compiler can safely do is copy the components that make up an object.
  63. That's what the compiler generated assignment operator does. 
  64.  
  65. If you want to override that you can. Just copy to the "this" pointer.
  66.  
  67. CLASS& operator=( CLASS &other )
  68. {
  69.     memcpy( this, &other, sizeof( CLASS ) );
  70. }
  71.  
  72. This is terrible C++ though.  Just don't do it. Take my word for it.
  73.  
  74.  
  75.  
  76.